package com.qidian.zl.paper.dao.impl;

import com.qidian.zl.paper.dao.BaseDAO;
import com.qidian.zl.paper.dao.FieldDAO;
import com.qidian.zl.paper.pojo.Field;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FieldDAOImpl extends BaseDAO implements FieldDAO {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public Long save(Field field, String[] selectorTexts) {
        Long fieldId = -1L;
        String sql = "insert into field (field_name,field_type,field_desc,field_status,paper_id) values (?,?,?,default ,? )";
        try {
            con = getCon();
            con.setAutoCommit(false);// 开启事务
            pst = con.prepareStatement(sql);
            pst.setString(1, field.getFieldName());
            pst.setInt(2, field.getFieldType());
            pst.setString(3, field.getFieldDesc());
            pst.setLong(4, field.getPaperId());
            pst.executeUpdate();
            // 获取当前事务中刚刚自动生成的id
            sql = "select LAST_INSERT_ID()";
            //再编译，在执行
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            if (rs.next()) {
                fieldId = rs.getLong(1);
            }
            //是否保存选项
            if (fieldId != -1 && (field.getFieldType() == 4 || field.getFieldType() == 5) && selectorTexts != null && selectorTexts.length >= 0) {
                sql = "insert into selector (selector_text,field_id) values (?,?)";
                pst = con.prepareStatement(sql);
                //循环的的设置参数和保存
                for (String text : selectorTexts) {
                    pst.setString(1, text);
                    pst.setLong(2, fieldId);
                    pst.executeUpdate();
                }
            }
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        } finally {
            closeAll(rs, pst, con);
        }
        return fieldId;
    }

    @Override
    public Long update(Field field, String[] selectorTexts) {
        String sql = "update field set field_name=?,field_desc=? where field_id=?";
        try {
            con = getCon();
            con.setAutoCommit(false);
            pst = con.prepareStatement(sql);
            pst.setString(1, field.getFieldName());
            pst.setString(2, field.getFieldDesc());
            pst.setLong(3, field.getFieldId());
            pst.executeUpdate();

            if (field.getFieldId() != -1 && (field.getFieldType() == 4 || field.getFieldType() == 5) && selectorTexts != null && selectorTexts.length >= 0) {
                sql = "delete from selector where field_id=?";
                pst = con.prepareStatement(sql);
                pst.setLong(1, field.getFieldId());
                pst.executeUpdate();

                sql = "insert into selector(select_text,field_id) values (?,?)";
                con.prepareStatement(sql);
                //循环的的设置参数和保存
                for (String text : selectorTexts) {
                    pst.setString(1, text);
                    pst.setLong(2, field.getFieldId());
                    pst.executeUpdate();
                }

            }
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        } finally {
            closeAll(rs, pst, con);
        }
        return field.getFieldId();
    }

    @Override
    public void delete(Long fieldId) {
        String sql = "delete from selector where field_id=?";
        try {
            con = getCon();
            con.setAutoCommit(false);
            pst = con.prepareStatement(sql);
            pst.setLong(1, fieldId);
            pst.executeUpdate();

            sql = "delete from field where field_id=?";
            pst = con.prepareStatement(sql);
            pst.setLong(1, fieldId);
            pst.executeUpdate();
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        } finally {
            closeAll(rs, pst, con);
        }

    }

    @Override
    public List<Field> queryByPaperId(Long paperId) {
        List<Field> fields = new ArrayList<>();
        String sql = "select * from field where paper_id = ?";
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setLong(1, paperId);
            rs = pst.executeQuery();
            while (rs.next()) {
                Field field = new Field();
                field.setFieldId(rs.getLong("field_id"));
                field.setFieldDesc(rs.getString("field_desc"));
                field.setFieldName(rs.getString("field_name"));
                field.setFieldStatus(rs.getInt("field_status"));
                field.setFieldType(rs.getInt("field_type"));
                field.setPaperId(paperId);
                fields.add(field);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            closeAll(rs, pst, con);
        }
        return fields;
    }
}
